const objectDate = function (val, format = 'yyyy-MM-dd-hh:mm') {
  if (val) {
    var date = new Date(val * 1);
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    let o = {
      'M+': date.getMonth() + 1,
      'd+': date.getDate(),
      'h+': date.getHours(),
      'm+': date.getMinutes(),
      's+': date.getSeconds()
    };
    for (let k in o) {
      if (new RegExp(`(${k})`).test(format)) {
        let str = o[k] + '';
        format = format.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : ('00' + str).substr(str.length));
      }
    }
    return format;
  }
  return val
}
function ChineseWeek (val) {
  switch (val) {
    case 1:
      return '星期一'
    case 2:
      return '星期二'
    case 3:
      return '星期三'
    case 4:
      return '星期四'
    case 5:
      return '星期五'
    case 6:
      return '星期六'
    case 7:
      return '星期日'
  }
}
const getDayWeek = function (val = new Date()) {
  let dayArray = new Array();
  for (let i = 0; i < 7; i++) {
    dayArray.push({
      Time: val.getTime(),
      TimeAll: objectDate(val.getTime() + '', 'MM-dd'),
      TimeDetail: objectDate(val.getTime() + '', 'yyyy-MM-dd'),
      year: val.getFullYear(),
      month: val.getMonth() + 1,
      regnDay: val.getDay() * 1 ? val.getDay() * 1 : 7,
      date: val.getDate(),
      hours: val.getHours(),
      ChineseWeek: ChineseWeek(val.getDay() * 1 ? val.getDay() * 1 : 7)
    })
    val = new Date(val.setDate(val.getDate() + 1))

  }
  return dayArray
}

// 获取字典的lable 方法
/**
 * 
 * @param {*} Dictname  字典名
 * @param {*} value     staue
 * @param {*} all     全部
 */
const $Dict = function (Dictname, value, all = false) {
  let Dict = uni.getStorageSync('Dict');
  if (value === undefined || value === null || value === '') {
    return Dict[Dictname]
  }
  if (Dict[Dictname]) {
    let data = Dict[Dictname].find((v) => {
      return v.value == value
    })
    if (all) {
      return data
    }
    return data.label
  }
}


/**
 *  根据当天时间获取本周的时间
 */
const getDates = function (new_Date = new Date()) {
  let timesStamp = new_Date.getTime();
  let currenDay = new_Date.getDay();
  let dates = [];
  for (let i = 0; i < 7; i++) {
    let date = new Date(timesStamp + 24 * 60 * 60 * 1000 * (i - (currenDay + 6) % 7));
    dates.push({
      date: dateFormat(date, 'yyyy-MM-dd'),
      week: date.getDay() * 1 ? date.getDay() * 1 : 7,
      ChineseWeek: ChineseWeek(date.getDay() * 1 ? date.getDay() * 1 : 7)
    });
  }
  return dates
}

/**
 * 根据当天时间获取本月的开始时间结束时间
 */
const getDateStaEnd = function (new_Date = new Date()) {
  let fullYear = new_Date.getFullYear();
  let month = new_Date.getMonth(); // getMonth 方法返回 0-11，代表1-12月
  let [endDay, beginDay] = [new Date(fullYear, month + 1, 0), new Date(fullYear, month, 1)]
  return {
    endDay: dateFormat(endDay, 'yyyy-MM-dd'),
    beginDay: dateFormat(beginDay, 'yyyy-MM-dd')
  }
}


/**
 * @description 判断是否为Json 是的话转为Object 不是  返回原来的值
 * @param all
 * @returns Object
 */
const isJson = function (data) {
  try {
    if (typeof (data) == 'string') {
      if (data.includes('[')) {
        return JSON.parse(data);
      }
      if (data.includes('{')) {
        return JSON.parse(data);
      }
    }
    return data;
  }
  catch (err) {
    return data
  }
}

export {
  objectDate,
  getDayWeek,
  serveModule,
  moduleControl,
  $Dict,
  ChineseWeek,
  getDates,
  getDateStaEnd,
  getStatus,
  dateFormat,
  isJson,
}